package api

import (
	"api-getway/pkg/qiniuyun"
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
)

func Upload(c *gin.Context) {
	file, err := c.FormFile("video")

	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "参数错误",
			Data: err.Error(),
		})
		return
	}

	upload, err := qiniuyun.Upload(file)

	//校验视频大小
	if file.Size > 1024*1024*1024 {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "视频大小不能大于1GB",
		})
		return
	}

	//校验视频格式
	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "视频格式必须为.mp4",
		})
		return
	}

	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "视频上传失败",
			Data: err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, &ResPonce{
		Code: 200,
		Msg:  "视频上传成功",
		Data: upload,
	})
}
